package com.mindcraft.user.api;

import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.user.po.McUser;
import com.mindcraft.user.serivce.WxAuthService;

import com.mindcraft.user.vo.WXUserVO;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Slf4j
@Controller
@RequestMapping("/user")
public class WxLoginController {

    @Autowired
    WxAuthService wxAuthService;

    @RequestMapping("/wxLogin")
    public String wxLogin(String code, String state) throws IOException {
        log.debug("微信扫码回调,code:{},state:{}", code, state);
        //请求微信申请令牌，拿到令牌查询用户信息，将用户信息写入本项目数据库
        WXUserVO wxUserVO = wxAuthService.wxAuth(code);
        if (wxUserVO == null) {
            return "redirect:http://www.51xuecheng.cn/error.html";
        }

        String userId = wxUserVO.getId();
        String name = wxUserVO.getName();
        String accessToken = wxUserVO.getAccessToken();
        String userpic = wxUserVO.getUserpic();

        return "redirect:http://127.0.0.1:5173/callback?id="+userId+"&username=" + name +"&userToken="+accessToken+"&avatar="+userpic+ "&authType=wx";
    }


}